﻿/**
 * 省市县镇联动
 */
var provinceContent = "";
(function ($) {
    'use strict';
    
    $(function () {    
                
    	getProvince();      

        $('.province').chosen().change(function () {
        	$('.city').empty("").trigger('chosen:updated').attr("value","");
        	$('.area').empty("").trigger('chosen:updated').attr("value","");
        	$('.town').empty("").trigger('chosen:updated').attr("value","");
            //加载市
            var province = $('.province').val();
            getCity(province);
        });

        $('.city').chosen().change(function () {
        	$('.area').empty("").trigger('chosen:updated');
        	$('.town').empty("").trigger('chosen:updated');
            var cityId = $('.city').val();
            getArea(cityId);
                
        });
        $('.area').chosen().change(function () {
        	$('.town').empty("").trigger('chosen:updated');
            var countyId = $('.area').val();
            getTown(countyId);
        });
        
        $('.town').chosen();

    }); //end $(function())

})(jQuery);

function getProvince(){
    $.ajax({
        type : "post",
        url :  "address/province",
        dataType : "json",
        success : function(resultdata) {
        	  var cityContent ="<option value='' >请选择</option>";
        	  for (var i = 0; i < resultdata.length; i++) {
                  provinceContent += "<option value='" + resultdata[i].provinceId + "' >" + resultdata[i].provinceName + "</option>";
              }
        	  $('.province').append(provinceContent).trigger('chosen:updated');
        	  var pSelect = $("#provinceId").attr("value");
              if(pSelect != ""){
              	$(".province").val(pSelect);
              	$('.province').trigger('chosen:updated');
              	getCity(pSelect)
              }
        },
        error : function(errorMsg) {
            alert("服务器未响应,请稍后再试");
        }
    });
	
}

function getCity(provinceId){
    $.ajax({
        type : "get",
        url :  "address/city?provinceId="+provinceId,
        success : function(resultdata) {
        	 var cityContent ="<option value='' >请选择</option>";
        	 for (var i = 0; i < resultdata.length; i++) {
        		 cityContent += "<option value='" + resultdata[i].cityId + "' >" + resultdata[i].cityName + "</option>";
             }
        	 $('.city').append(cityContent).trigger('chosen:updated');
        	 var pSelect = $(".city").attr("value");
        	 console.log(pSelect);
             if(pSelect != ""){
             	$(".city").val(pSelect);
             	$('.city').trigger('chosen:updated');
             	getArea(pSelect)
             }
        	
        },
        error : function(errorMsg) {
            alert("服务器未响应,请稍后再试");
        }
    });
	
}

function getArea(cityId){
    $.ajax({
        type : "get",
        url :  "address/county?cityId="+cityId,
        success : function(resultdata) {
        	 var cityContent ="<option value='' >请选择</option>";
        	 for (var i = 0; i < resultdata.length; i++) {
        		 cityContent += "<option value='" + resultdata[i].countyId + "' >" + resultdata[i].countyName + "</option>";
             }
        	 $('.area').append(cityContent).trigger('chosen:updated');
        	 var pSelect = $(".area").attr("value");
             if(pSelect != ""){
             	$(".area").val(pSelect);
             	$('.area').trigger('chosen:updated');
             	getTown(pSelect)
             }
        },
        error : function(errorMsg) {
            alert("服务器未响应,请稍后再试");
        }
    });
}

function getTown(countyId){
    $.ajax({
        type : "get",
        url :  "address/town?countyId="+countyId,
        success : function(resultdata) {
        	 var cityContent ="<option value='' >请选择</option>";
        	 for (var i = 0; i < resultdata.length; i++) {
        		 cityContent += "<option value='" + resultdata[i].townId + "' >" + resultdata[i].townName + "</option>";
             }
        	 $('.town').append(cityContent).trigger('chosen:updated');
        	 var pSelect = $(".town").attr("value");
             if(pSelect != ""){
             	$(".town").val(pSelect);
             	$('.town').trigger('chosen:updated');
             }
        },
        error : function(errorMsg) {
            alert("服务器未响应,请稍后再试");
        }
    });
}

